#!/bin/bash

# Slackware build script for yvonne

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This is a Qt4 app, from 2006. Was never ported to Qt5. I may
# eventually port it myself, but Qt and C++ are not my favorite things
# to work with. For now, it's submitted as a qt4 app, and it works.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=yvonne
VERSION=${VERSION:-1.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# The display window doesn't actually start out all black; there's
# garbage that gets displayed outside the image area. This sets
# all the pixels to black, gets rid of the garbage. Probably there's
# a more elegant way to fix this, but it does work.
patch -p1 < $CWD/fixgarbage.diff

# Fix the compiler warnings about converting string constants to char *.
# Not actually necessary, but it helped me with the garbage fix, above.
patch -p1 < $CWD/fixwarnings.diff

# One source file has an ISO-8859-2 character (Polish l-with-slash)
# embedded in it, which displays incorrectly (as a superscript 3) in
# the Help/About dialog. Converting to UTF-8 doesn't actually help:
# it shows up as two wrong characters instead of one.
# Qt4 only knows ISO-8859-1 (Latin-1), which doesn't have the
# character in question... so convert it to a regular lowercase l,
# which at least looks closer to correct.
patch -p1 < $CWD/fixencoding.diff

# Add a missing library, and use our flags.
sed -i -e '/^LFLAGS/s,$, -lQtCore,' \
       -e "/^CFLAGS/s,=,=$SLKCFLAGS ," \
       Makefile

# This will fail with "qt4.sh: no such file or directory" and kill the
# shell due to "set -e", if the user forgot to install the required
# qt4 dep.
source /etc/profile.d/qt4.sh
make $PRGNAM

mkdir -p $PKG/usr/bin
install -s -m0755 $PRGNAM $PKG/usr/bin

# Use upstream's icon, even though it's a pretty generic Atari logo.
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps $PKG/usr/share/pixmaps
cp -a icon.png $PKG/usr/share/icons/hicolor/48x48/apps/$PRGNAM.png
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# .desktop file written by SlackBuild author.
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# Not much documentation (just the GPL), so include our own README.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a copying $PKGDOC
cat $CWD/README > $PKGDOC/README
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
